var dictKeyArray = [];
layui.use(['element','table','form','layer'], function(){
    var table = layui.table,
        form = layui.form,
        $ = layui.jquery,
        layer = layui.layer;

    table.render({
        elem: '#deviceTable'
        ,url:'/faceAcsDevice/list'
        ,title: '人脸设备管理'
        ,where: {name:''}
        ,cols: [[
            {field: 'xh',type:'numbers', title: '序号',}
            ,{field:'serialNum', title:'设备序列号',align:'center'}
            ,{field:'deviceMac', title:'MAC',align:'center'}
            // ,{field:'deviceIp', title:'IP',align:'center'}
            ,{field:'deviceName', title:'设备别名',align:'center'}
            ,{field:'deviceArea', title:'进出方向',align:'center'}
            ,{field:'status', title:'状态',align:'center',templet:function (data) {
                    return parseStatus(data.status);
                }}
            ,{field:'createTime', title:'创建时间',  sort: true,templet:
                    function(data){
                        return formatTime(data.createTime);
                    }}
            ,{field:'right',fix:'right',title:'操作',toolbar:'#barOperate',align:'center',width:250}
        ]]
        ,page: true
    });

    function formatTime(data){
        return data ? data.replace("T", " "):"";
    }

    table.on('tool(deviceTable)',function (obj) {
        var data = obj.data;
        switch (obj.event) {
            case 'edit':
                $("#upload_form").attr("disabled", false);
                $("#upload_form").attr("class", "layui-btn");
                $("#id").val(data.id);
                $("#deviceName").val(data.deviceName)
                $("#deviceArea").val(data.deviceArea);
                $("#password").val(data.password);
                form.render();
                layer.open({
                    type: 1
                    , title: '编辑门禁设备'
                    , area: ['40%', '60%']
                    , content: $('#uploadDiv')
                });
                break;
            case 'delete':
                layer.confirm('确定要删除吗?', {
                }, function(){
                    layer.load();
                    $.get('/faceAcsDevice/delete',{id:data.id},function (res) {
                        layer.msg(res.msg,{
                            time:500,
                            end:function () {
                                table.reload('deviceTable');
                                layer.closeAll();
                            }
                        });
                    });
                });
                break;
            case 'open':
                layer.confirm('确定要开门吗?', {
                }, function(){
                    layer.load();
                    var params = {serialNum:data.serialNum,productCode:data.productCode,password:data.password};
                    $.get('/faceAcsDevice/open',params,function (res) {
                        layer.msg(res.msg,{
                            time:500,
                            end:function () {
                                layer.closeAll();
                            }
                        });
                    });
                });
                break;
            case 'setPassword':
                $("#setPassword").attr("disabled", false);
                $("#setPassword").attr("class", "layui-btn");
                $("#setPasswordId").val(data.id);
                $("#password").val(data.password);
                form.render();
                layer.open({
                    type: 1
                    , title: '设置密码'
                    , area: ['40%', '50%']
                    , content: $('#setPasswordDiv')
                });
                break;
        }
    });

    form.on('submit(query_device)',function () {
        var name = $("#name").val();
        table.reload('deviceTable',{
            where: {name:name}
            ,page:  {
                curr:1
            }
        });
        return false;
    });

    $("#syncDevice").on('click', function (){
        layer.confirm('同步数据，是否继续？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            var index = layer.load(2, {
                shade: [0.4, '#000'],
                content: '同步中...',
                success: function (layero) {
                    layero.find('.layui-layer-content').css({
                        'padding-top': '40px',
                        'width': '200px'
                    });
                }
            });
            $.post('/faceAcsDevice/syncData',{},function (res) {
                if(res.status === 200){
                    layer.msg(res.msg,{
                        time:1000,
                        end:function () {
                            reload();
                        }
                    });
                }else{
                    layer.msg(res.msg);
                }
                layer.close(index);
            });
        })
    });

    form.on('submit(upload_form)',function(data){
        $("#upload_form").attr("disabled", true);
        $("#upload_form").attr("class", "layui-btn layui-btn-disabled");
        layer.load();
        $.ajax({
            type: 'post',
            url: '/faceAcsDevice/saveOrUpdate',
            data: data.field,
            success: function (res) {
                layer.msg(res.msg);
                setTimeout(function (){
                    layer.closeAll();
                },500);
                $("#upload_form").attr("disabled", false);
                $("#upload_form").attr("class", "layui-btn");
                table.reload('deviceTable');
            },
        });
        return false;
    });
    form.on('submit(setPassword)',function(data){
        $("#setPassword").attr("disabled", true);
        $("#setPassword").attr("class", "layui-btn layui-btn-disabled");
        layer.load();
        $.ajax({
            type: 'post',
            url: '/faceAcsDevice/saveOrUpdate',
            data: data.field,
            success: function (res) {
                layer.msg(res.msg);
                setTimeout(function (){
                    layer.closeAll();
                },500);
                $("#setPassword").attr("disabled", false);
                $("#setPassword").attr("class", "layui-btn");
                table.reload('deviceTable');
            },
        });
        return false;
    });
    //远程开门
    form.on('submit(open_form)',function(data){
        layer.confirm('确定要开门吗?', {
        }, function(){
            var serialNum = $("#serialNum").val();
            var productCode = $("#productCode").val();
            var password = $("#password").val();
            layer.load();
            var params = {serialNum:serialNum,productCode:productCode,password:password};
            $.get('/faceAcsDevice/open',params,function (res) {
                layer.msg(res.msg,{
                    time:500,
                    end:function () {
                        layer.closeAll();
                    }
                });
            });
        });
        return false;
    });

    $("#cancel_form").on('click',function (){
        layer.closeAll();
        return false;
    });
    $("#cancelSetPassword").on('click',function (){
        layer.closeAll();
        return false;
    });

    $("#cancel_open").on('click',function (){
        layer.closeAll();
        return false;
    });
});

function parseStatus(status) {
    if(typeof status == "undefined" || status == null){
        return "未知";
    }
    switch (Number(status)) {
        case 0 : return "在线";
        case 1 : return "离线";
        default: return "未知";
    }

}
function reload(){
    location.reload();
}
